using Medical.Api.Write.Application.Command.RecyclingCleaning;
using Medical.Api.Write.Service.RecyclingCleaning;
using Medical.Domain.RecoveryCleaning;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;
using MediatR;
using Microsoft.Extensions.Logging;

namespace Medical.Api.Write.Application.Handler.RecyclingCleaning
{
    /// <summary>
    /// 修改清洗登记处理器
    /// </summary>
    public class UpdateCleaningRegistrationCommandHandler : IRequestHandler<UpdateCleaningRegistrationCommand, ApiResult<int>>
    {
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IBaseRepository<CleaningRegistration> cleaningRegistrationRepository;

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<UpdateCleaningRegistrationCommandHandler> logger;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="cleaningRegistrationRepository">清洗登记仓储</param>
        /// <param name="logger">日志</param>
        /// <param name="recyclingCleaningService">回收清理服务</param>
        public UpdateCleaningRegistrationCommandHandler(
            IBaseRepository<CleaningRegistration> cleaningRegistrationRepository,
            ILogger<UpdateCleaningRegistrationCommandHandler> logger)
        {
            this.cleaningRegistrationRepository = cleaningRegistrationRepository;
            this.logger = logger;
        
        }

        /// <summary>
        /// 处理修改清洗登记请求
        /// </summary>
        /// <param name="request">修改清洗登记命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>返回受影响行数</returns>
        public async Task<ApiResult<int>> Handle(UpdateCleaningRegistrationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 根据登记清洗Id获取要修改的清洗登记
                var cleaningRegistration = cleaningRegistrationRepository.GetAll().FirstOrDefault(x => x.Id == request.Id && !x.IsDeleted);

                if (cleaningRegistration == null)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Fail,
                        Data = 0,
                        Msg = $"未找到ID为{request.Id}的清洗登记"
                    };
                }

                // 更新清洗登记信息
                cleaningRegistration.CleaningBatch = request.CleaningBatch;
                cleaningRegistration.Cleaner = request.Cleaner;
                cleaningRegistration.StartTime = request.StartTime;
                cleaningRegistration.CleaningMode = request.CleaningMode;
                cleaningRegistration.CleaningEquipment = request.CleaningEquipment;
                cleaningRegistration.ItemIds = request.ItemIds;
                // 更新修改时间
                cleaningRegistration.UpdateTime = DateTime.Now;
                // 保存更改
                var result =  cleaningRegistrationRepository.Update(cleaningRegistration);

                if (result > 0)
                {
                    logger.LogInformation($"成功修改ID为{request.Id}的清洗登记");
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Success,
                        Data = result,
                        Msg = "修改清洗登记成功"
                    };
                }
                else
                {
                    logger.LogWarning($"修改ID为{request.Id}的清洗登记失败");
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Fail,
                        Data = 0,
                        Msg = "修改清洗登记失败"
                    };
                }
            }
            catch (Exception ex)
            {
                logger.LogError($"修改清洗登记异常: {ex.Message}");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Data = 0,
                    Msg = $"修改清洗登记异常: {ex.Message}"
                };
            }
        }
    }
}